package com.llb.utils;

import com.llb.entity.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ExcelReader
 * @Description TODO
 * @Author liang
 * @Date 2019/3/22 22:19
 * @Version 1.0
 **/
public class ExcelReader {
    private FileInputStream inputStream;
    private Workbook workbook;
    private Sheet sheet;
    private Row row;
    private int titleNum = 1;


    /**
     * 读取excel内容
     * @param path
     * @param clazz
     * @return
     */
    public  List<Object> readExcelContent (String path,Class clazz) throws IOException, IllegalAccessException, InstantiationException, InvocationTargetException {
        boolean matches = path.matches("^.+\\.(?i)((xls)|(xlsx))$");
        //判断03版或07版Excel
        if(matches){
            inputStream = new FileInputStream(path);
            boolean is03Excell = path.matches("^.+\\.(?i)(xls)$") ? true : false;
            workbook = is03Excell ? new HSSFWorkbook(inputStream) : new XSSFWorkbook(inputStream);
        }
        sheet = workbook.getSheetAt(0);
        List<Object> objects = readExcelContentBySheet(sheet, clazz);

        return objects;
    }

    /**
     * 读取资源文件中码表
     * @param inputStream
     * @return
     * @throws IOException
     * @throws IllegalAccessException
     * @throws InstantiationException
     * @throws InvocationTargetException
     */
    public  Map<String,List<Object>>  readExcelContent (InputStream inputStream ) throws IOException, IllegalAccessException, InstantiationException, InvocationTargetException {
        HashMap<String, List<Object>> dmMap = new HashMap<String, List<Object>>();
        workbook =  new XSSFWorkbook(inputStream);
        Sheet DM_SBF_RS_XZQH_Sheet = sheet = workbook.getSheet("DM_SBF_RS_XZQH");
        Sheet DM_SBF_RS_XZLX_Sheet = sheet = workbook.getSheet("DM_SBF_RS_XZLX");
        Sheet CS_SBF_APP_Sheet = sheet = workbook.getSheet("CS_SBF_APP");
        Sheet DM_SBF_SBJBJG_Sheet = sheet = workbook.getSheet("DM_SBF_SBJBJG");

        List<Object> DM_SBF_RS_XZQH = readExcelContentBySheet(DM_SBF_RS_XZQH_Sheet, DM_SBF_RS_XZQH.class);
        List<Object> DM_SBF_RS_XZLX = readExcelContentBySheet(DM_SBF_RS_XZLX_Sheet, DM_SBF_RS_XZLX.class);
        List<Object> CS_SBF_APP = readExcelContentBySheet(CS_SBF_APP_Sheet, CS_SBF_APP.class);
        List<Object> DM_SBF_SBJBJG = readExcelContentBySheet(DM_SBF_SBJBJG_Sheet, DM_SBF_SBJBJG.class);

        dmMap.put("DM_SBF_RS_XZQH",DM_SBF_RS_XZQH);
        dmMap.put("DM_SBF_RS_XZLX",DM_SBF_RS_XZLX);
        dmMap.put("CS_SBF_APP",CS_SBF_APP);
        dmMap.put("DM_SBF_SBJBJG",DM_SBF_SBJBJG);

        return dmMap;
    }


    public List<Object> readExcelContentBySheet(Sheet sheet,Class clazz) throws IllegalAccessException, InstantiationException, InvocationTargetException {
        ArrayList returnList = new ArrayList();
        int rowNum = this.sheet.getLastRowNum();//获取最后一行,从0开始算
        //int rowNum2 = this.sheet.getPhysicalNumberOfRows();//获取的是物理行数
        row = sheet.getRow(0);
        //将每行解析为对象放入list
        for(int i=0;i <= rowNum;i++){
            if(i >= titleNum){
                row = sheet.getRow(i);
                if(row != null){
                    //将每行数据转成对象
                    Object o = ParserUtil.rowConverToEntity(row, clazz);
                    if(o != null){
                        returnList.add(o);
                    }
                }
            }
        }
        return returnList;
    }


}
